package cc.risingsun.wing.core.scanner;

import java.io.IOException;
import java.io.InputStream;

import org.objectweb.asm.ClassReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cc.risingsun.wing.core.AnnotatedClassVisitor;

/**
 * 
 * @author iron.yin@gmail.com
 */
public class AnnotatedScannerListener implements ScannerListener {

	private static Logger logger = LoggerFactory.getLogger(AnnotatedScannerListener.class);

	private AnnotatedClassVisitor classVisitor;

	public AnnotatedScannerListener(AnnotatedClassVisitor annotatedClassVisitor) {
		this.classVisitor = annotatedClassVisitor;
	}

	@Override
	public boolean onAccept(String resource) {
		return resource.endsWith(".class");
	}

	@Override
	public void onProcess(String resource, InputStream input) throws IOException {
		try {
			new ClassReader(input).accept(classVisitor, 0);
		} catch (ScannerSkipException e) {
			// 这里做switch 是为了方便以后统一分类管理异常
			logger.warn(e.getMessage());
			switch (e.getSkipConde()) {
			case NONPUBLIC:
				break;
			case NONCONTAIN:
				break;
			case NONINTERFACE:
				break;
			default:
				break;
			}
		}
	}

	@Override
	public void onProcess(String resource, byte[] input) throws IOException {
		new ClassReader(input).accept(classVisitor, 0);
	}

}
